﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using VirtualDrum.lib;
using VirtualDrum.lib.db;
using VirtualDrum.util;

namespace VirtualDrum.componets
{
	public partial class frmOptions : Form
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		public frmOptions()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 添加音乐事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddMusic_Click(object sender, EventArgs e)
		{
			if (this.getCurCBDrumGroup().Equals(""))
			{
				MessageBox.Show("请选择鼓组！");
				return;
			}
			frmMusicInfo frmMusicInfo = new frmMusicInfo();
			frmMusicInfo.MusicDrumGroup = this.getCurCBDrumGroup();
			frmMusicInfo.Show();
		}

		/// <summary>
		/// 窗体加载
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void frmOptions_Load(object sender, EventArgs e)
		{
			initLBDrumGroup();
			initCHDrumMusic(null);
		}

		/// <summary>
		/// 初始化鼓组列表
		/// </summary>
		private void initLBDrumGroup(){
			this.lbDrumGroup.Items.Clear();
			this.cbDrumGroup.Items.Clear();
			IList<string[]> alQueryResult = this.getAllDrumGroup();
			IEnumerator ieResult = alQueryResult.GetEnumerator();
			while (ieResult.MoveNext())
			{
				object[] arrStr = (object[])ieResult.Current;
				this.lbDrumGroup.Items.Add(arrStr[0] + ":" + arrStr[1]);
				this.cbDrumGroup.Items.Add(arrStr[0]);
			}
		}

		/// <summary>
		/// 初始化鼓组对应的音源列表
		/// </summary>
		/// <param name="drumGroup"></param>
		private void initCHDrumMusic(string drumGroup){
			this.clbMusic.Items.Clear();
			IList<string[]> alQueryResult = this.getAllDrumMusic(drumGroup);
			IEnumerator ieResult = alQueryResult.GetEnumerator();
			while (ieResult.MoveNext())
			{
				object[] arrStr = (object[])ieResult.Current;
				this.clbMusic.Items.Add(arrStr[0] + ":" +arrStr[1]);
			}
		}

		/// <summary>
		/// 添加鼓组按键事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddDrumGroup_Click(object sender, EventArgs e)
		{
			frmDrumGroupInfo frmDrumGroupInfo = new frmDrumGroupInfo();
			frmDrumGroupInfo.ActionName = "add";
			frmDrumGroupInfo.Show();
		}

		/// <summary>
		/// 修改鼓组按键事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChangeDrumGroup_Click(object sender, EventArgs e)
		{
			if (false == this.checkCurDrumGroupSelected()) return;
			frmDrumGroupInfo frmDrumGroupInfo = new frmDrumGroupInfo();
			frmDrumGroupInfo.ActionName = "change";
			frmDrumGroupInfo.GroupName = this.getItemName(this.getCurLBDrumGroup());
			frmDrumGroupInfo.GroupMusicPath = this.getItemPath(this.getCurLBDrumGroup());
			frmDrumGroupInfo.Show();
		}

		/// <summary>
		/// 返回主程序按键事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnGoToMain_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		/// <summary>
		/// 得到所有的鼓组信息列表
		/// </summary>
		/// <returns></returns>
		private IList<string[]> getAllDrumGroup()
		{
			string sql = "SELECT [groupName],[groupMusicPath] FROM [vd_drumgroup] order by [groupName];";
			return DBServer.executeQuery(sql);
		}

		/// <summary>
		/// 得到所有的鼓组对应音乐列表
		/// </summary>
		/// <param name="drumGroup"></param>
		/// <returns></returns>
		private IList<string[]> getAllDrumMusic(string drumGroup)
		{
			string sql = "SELECT [musicName],[musicPath] FROM [vd_drummusic]";
			sql += drumGroup == null ? null : " WHERE [musicGroupName] = '" + drumGroup + "'";
			sql += " ORDER BY [musicID]";
			return DBServer.executeQuery(sql);
		}

		/// <summary>
		/// 刷新按钮对应事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRefreshDrumGroup_Click(object sender, EventArgs e)
		{
			this.initLBDrumGroup();
		}
		
		/// <summary>
		/// 删除当前选中鼓组
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteDrumGroup_Click(object sender, EventArgs e)
		{
			if(false == this.checkCurDrumGroupSelected()) return;
			string groupName = this.getItemName(this.getCurLBDrumGroup());

			if (0 < frmOptions.delDrumGroup(groupName) && 0 < frmOptions.delDrumGroup(groupName))
			{
				MessageBox.Show("删除成功！");
			}else{
				MessageBox.Show("删除失败！");
			}			
		}

		/// <summary>
		/// 得到当前鼓组名
		/// </summary>
		/// <returns></returns>
		private string getItemName(string oldString){
			return LittleTools.getItemNameStartSplitByChar(oldString, ':');
		}

		/// <summary>
		/// 得到当前鼓组主音乐路径
		/// </summary>
		/// <returns></returns>
		private string getItemPath(string oldString){
			return LittleTools.getItemNameEndSplitByChar(oldString, ':');
		}

		/// <summary>
		/// 删除鼓组信息及鼓组所对应的音源文件
		/// </summary>
		/// <param name="groupName"></param>
		/// <returns></returns>
		public static int delDrumGroup(string groupName)
		{
			string sql = string.Format("DELETE FROM [vd_drumgroup] WHERE [groupName] = '{0}';", groupName);
			if (DBServer.executeNonQuery(sql) > 0)
			{
				return frmOptions.delteDrumGroupMusic(groupName);
			}
			else
			{
				return -1;
			}
		}

		/// <summary>
		/// 删除当前所选鼓组
		/// </summary>
		/// <param name="groupName"></param>
		/// <returns></returns>
		public static int delteDrumGroupMusic(string groupName){
			string sql = string.Format("DELETE FROM [vd_drummusic] WHERE [musicGroupName] = '{0}';", groupName);
			return DBServer.executeNonQuery(sql);
		}

		/// <summary>
		/// 得到当前的鼓组字符串
		/// </summary>
		/// <returns></returns>
		private string getCurLBDrumGroup(){
			return this.lbDrumGroup.Items[this.lbDrumGroup.SelectedIndex].ToString();
		}

		/// <summary>
		/// 得到当前CheckBox里的所选鼓组
		/// </summary>
		/// <returns></returns>
		private string getCurCBDrumGroup(){
			if(this.cbDrumGroup.SelectedIndex < 0) return "";
			return this.cbDrumGroup.Items[this.cbDrumGroup.SelectedIndex].ToString();
		}

		/// <summary>
		/// 当checkbox里的选择改变时就发生些事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cbDrumGroup_SelectedIndexChanged(object sender, EventArgs e)
		{
			if(this.cbDrumGroup.SelectedIndex < 0){
				Console.WriteLine(this.cbDrumGroup.SelectedIndex);
				return;
			}
			this.initCHDrumMusic(this.cbDrumGroup.Items[this.cbDrumGroup.SelectedIndex].ToString());
		}

		/// <summary>
		/// 得到所先的音乐项
		/// </summary>
		/// <returns></returns>
		private string getCurMusicItem()
		{
			return this.clbMusic.Items[this.clbMusic.SelectedIndex].ToString();
		}

		/// <summary>
		/// 更改音乐信息按钮事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnChangeMusic_Click(object sender, EventArgs e)
		{
			if(false == this.checkCurMusicSelected()) return;
			this.editMusicItemInfo(this.getCurMusicItem());
		}

		/// <summary>
		/// 修改音乐事件
		/// </summary>
		/// <param name="musicInfo"></param>
		private void editMusicItemInfo(string musicInfo){
			frmMusicInfo frmMusicInfo = new frmMusicInfo();
			frmMusicInfo.MusicName = this.getItemName(musicInfo);
			frmMusicInfo.OldMusicName = frmMusicInfo.MusicName;
			frmMusicInfo.MusicPath = this.getItemPath(musicInfo);
			frmMusicInfo.ActionName = "change";
			frmMusicInfo.Show();
		}
		
		private static ControlDatabase  DBServer = new DBFactory().createDBServer();

		/// <summary>
		/// 删除当前所选的音乐
		/// </summary>
		/// <returns></returns>
		private int deleteDrumMusic(){
			string musicInfo = "";
			string sql = "DELETE FROM [vd_drummusic] WHERE";
			foreach(object musicItem in this.clbMusic.CheckedItems){
				musicInfo = musicItem.ToString();
				sql += string.Format(" [musicName] = '{0}' OR",this.getItemName(musicInfo));
			}
			if (!musicInfo.Equals("")){
				sql = sql.Substring(0, sql.Length -2);
			}
			return DBServer.executeNonQuery(sql);
		}

		/// <summary>
		/// 删除音乐按钮事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteGroupMusic_Click(object sender, EventArgs e)
		{
			if (false == this.checkCurMusicSelected()) return;
			int result = this.deleteDrumMusic();
			if (result  >= 0)
			{
				LittleTools.showMsg("删除成功！" + result + "个文件被删除", frmMain.projectName);
			}
			else
			{
				LittleTools.showMsg("删除失败！", frmMain.projectName);
			}
		}

		/// <summary>
		/// 检测当前是否有鼓组被选中
		/// </summary>
		/// <returns></returns>
		private Boolean checkCurDrumGroupSelected(){
			if(this.lbDrumGroup.SelectedIndex < 0){
				LittleTools.showMsg("请先选择鼓组！", frmMain.projectName);
				return false;
			}
			return true;
		}

		/// <summary>
		/// 检测是否有音源项被选中
		/// </summary>
		/// <returns></returns>
		private Boolean checkCurMusicSelected(){
			if (this.clbMusic.SelectedIndex <= 0)
			{
				MessageBox.Show("请选择修改项！");
				return false;
			}
			return true;
		}

		/// <summary>
		/// 刷新按键事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRefreshMusic_Click(object sender, EventArgs e)
		{
			this.initCHDrumMusic(null);
		}
	}
}
